﻿using MahApps.Metro.Controls;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Domain;
namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// DeliveryTemplateEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeliveryTemplateEditWindow : Window
    {
        public bool NeedUpdate { get; set; }
        public Mengsk.ProxyDelivery.Domain.DeliveryTemplate DeliveryTemplate { get; set; }
        private System.Collections.ObjectModel.ObservableCollection<DeliveryTemplateAreaVM> templateAreas = new System.Collections.ObjectModel.ObservableCollection<DeliveryTemplateAreaVM>();

        public DeliveryTemplateEditWindow()
        {
            InitializeComponent();
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DeliveryTemplate == null)
                {
                    this.Title = "新增运费模板";
                    this.DeliveryTemplate = new ProxyDelivery.Domain.DeliveryTemplate() { Areas = new List<ProxyDelivery.Domain.DeliveryTemplateArea>() };
                }
                else
                {
                    this.Title = "编辑运费模板";
                    this.tbName.Text = this.DeliveryTemplate.Name;
                }
                this.tbPaperMoney.Text = this.DeliveryTemplate.PaperMoney.ToString("F2");
                this.cbbDeliveryCompanies.ItemsSource = Domain.DeliveryCompanyHelper.GetDeliveryCompany();
                if (string.IsNullOrWhiteSpace(this.DeliveryTemplate.DeliveryCompany))
                {
                    this.cbbDeliveryCompanies.SelectedIndex = 0;
                }
                else
                {
                    this.cbbDeliveryCompanies.SelectedItem = this.DeliveryTemplate.DeliveryCompany;
                }
                foreach (var item in this.DeliveryTemplate.Areas.Select(obj => new DeliveryTemplateAreaVM { Source = obj }))
                {
                    this.templateAreas.Add(item);
                }
                this.cbbType.Bind<DeliveryTemplateType>();
                this.cbbType.SelectedItem = this.DeliveryTemplate.Type;
                this.dgvAreas.ItemsSource = this.templateAreas;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string name = this.tbName.Text.Trim();
            string deliveryCompany = this.cbbDeliveryCompanies.SelectedItem.ToString();
            string paperMoney = this.tbPaperMoney.Text.Trim();

            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(deliveryCompany))
            {
                MessageBox.Show("请输入运费模板名称与物流公司");
                return;
            }
            try
            {
                float money = float.Parse(paperMoney);

                this.DeliveryTemplate.Areas.Clear();
                foreach (var area in this.templateAreas)
                {
                    if (area.Source.Area == null)
                    {
                        area.Source.Area = string.Empty;
                    }
                    this.DeliveryTemplate.Areas.Add(area.Source);
                }
                this.DeliveryTemplate.UpdateTime = DateTime.Now;
                this.DeliveryTemplate.Name = name;
                this.DeliveryTemplate.DeliveryCompany = deliveryCompany;
                this.DeliveryTemplate.Operator = App.LoginOperator.Number;
                this.DeliveryTemplate.Warehouse = App.LoginOperator.Warehouse;
                this.DeliveryTemplate.PaperMoney = money;
                this.DeliveryTemplate.Type = this.cbbType.GetSelectedEnum<DeliveryTemplateType>();
                if (this.DeliveryTemplate.Id < 1)
                {
                    int id = Mengsk.ProxyDelivery.Service.ServiceContainer.GetService<DeliveryTemplateService>().Insert(this.DeliveryTemplate);
                    this.DeliveryTemplate.Id = id;
                }
                else
                {
                    Mengsk.ProxyDelivery.Service.ServiceContainer.GetService<DeliveryTemplateService>().Update(this.DeliveryTemplate);
                }
                this.NeedUpdate = true;
                MessageBox.Show("已成功保存");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddArea_Click(object sender, RoutedEventArgs e)
        {
            Mengsk.ProxyDelivery.Domain.DeliveryTemplateArea area = new ProxyDelivery.Domain.DeliveryTemplateArea();
            this.templateAreas.Add(new DeliveryTemplateAreaVM { Source = area });
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb == null)
            {
                throw new Exception("Edit_Click错误，事件源不是TextBlock");
            }
            DataGridCell dd = tb.Parent as DataGridCell;
            DeliveryTemplateAreaVM vm = dd.DataContext as DeliveryTemplateAreaVM;
            var window = new ProvincesSeletorWindow { Province = vm.Source.Area };
            bool? ret = window.ShowDialog();
            if (ret != null && ret.Value)
            {
                vm.Source.Area = window.Province;
                this.dgvAreas.ItemsSource = null;
                this.dgvAreas.ItemsSource = this.templateAreas;
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb == null)
            {
                throw new Exception("Edit_Click错误，事件源不是TextBlock");
            }
            DataGridCell dd = tb.Parent as DataGridCell;
            DeliveryTemplateAreaVM vm = dd.DataContext as DeliveryTemplateAreaVM;

            if (MessageBox.Show("删除:" + vm.Source.Area, "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
            {
                return;
            }

            this.templateAreas.Remove(vm);
        }
    }
}
